<template>
  <div style="margin-top:20px;">
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100" style="width: 500px; margin: auto">

      <FormItem style="text-align: center;">
        <h2 style="font-size: 25px; margin-bottom:5px;">简历编辑</h2>
        <Button type="dashed" @click="viewResume">查看</Button>
      </FormItem>


      <FormItem label="招聘信息来源" prop="jobOrigin">
        <Select v-model="formValidate.jobOrigin">
          <Option value="应届生求职网">应届生求职网</Option>
          <Option value="招聘会">招聘会</Option>
          <Option value="同学推荐">同学推荐</Option>
          <Option value="宣讲会">宣讲会</Option>
          <Option value="其他">其他</Option>
        </Select>
      </FormItem>

      <FormItem label="姓名" prop="realName">
        <Input v-model="formValidate.realName"/>
      </FormItem>

      <FormItem label="性别" prop="sex">
        <RadioGroup v-model="formValidate.sex">
          <Radio label="男">男</Radio>
          <Radio label="女">女</Radio>
        </RadioGroup>
      </FormItem>

      <FormItem label="现居住地" prop="hometown">
        <Input v-model="formValidate.hometown"/>
      </FormItem>

      <FormItem label="生日" prop="birthday">
        <DatePicker type="date" v-model="formValidate.birthday"></DatePicker>
      </FormItem>

      <FormItem label="政治面貌" prop="politics">
        <Input v-model="formValidate.politics"/>
      </FormItem>

      <FormItem label="学历" prop="degree">
        <Input v-model="formValidate.degree"/>
      </FormItem>

      <FormItem label="大学" prop="college">
        <Input v-model="formValidate.college"/>
      </FormItem>

      <FormItem label="手机号" prop="phone">
        <Input v-model="formValidate.phone"/>
      </FormItem>

      <FormItem label="邮箱" prop="email">
        <Input v-model="formValidate.email"/>
      </FormItem>

      <FormItem label="自我介绍" prop="selfDescription">
        <Input v-model="formValidate.selfDescription" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="介绍一下自己呗"/>
      </FormItem>

      <FormItem>
        <h3 class="sub_title">教育经历</h3>
      </FormItem>

      <div v-for="(item, index) in formValidate.education" :key="index">
        <FormItem label="学校" prop="education.school">
          <Input v-model="item.school"/>
        </FormItem>

        <FormItem label="专业" prop="education.major">
          <Input v-model="item.major"/>
        </FormItem>

        <Row>
          <i-col span="12">
            <FormItem label="开始时间" prop="education.begin_time">
              <DatePicker type="date" v-model="item.begin_time"></DatePicker>
            </FormItem>
          </i-col>
          <i-col span="12">
            <FormItem label="结束时间" prop="education.end_time">
              <DatePicker type="date" v-model="item.end_time"></DatePicker>
            </FormItem>
          </i-col>
        </Row>

        <FormItem>
          <Button type="dashed" long @click="handleRemove(formValidate.education, index)">删除</Button>
        </FormItem>
      </div>

      <FormItem>
        <Button type="dashed" long @click="handleAddEducation()" icon="plus-round">添加</Button>
      </FormItem>


      <FormItem>
        <h3 class="sub_title">实习经历</h3>
      </FormItem>

      <div v-for="(item, index) in formValidate.workExperience" :key="index">
        <FormItem label="公司" prop="workExperience.company">
          <Input v-model="item.company"/>
        </FormItem>

        <FormItem label="职位" prop="workExperience.job">
          <Input v-model="item.job"/>
        </FormItem>

        <FormItem label="职位描述" prop="workExperience.job_desc">
          <Input v-model="item.job_desc"/>
        </FormItem>

        <Row>
          <i-col span="12">
            <FormItem label="开始时间" prop="workExperience.begin_time">
              <DatePicker type="date" v-model="item.begin_time"></DatePicker>
            </FormItem>
          </i-col>
          <i-col span="12">
            <FormItem label="结束时间" prop="workExperience.end_time">
              <DatePicker type="date" v-model="item.end_time"></DatePicker>
            </FormItem>
          </i-col>
        </Row>

        <FormItem>
          <Button type="dashed" long @click="handleRemove(formValidate.workExperience, index)">删除</Button>
        </FormItem>
      </div>

      <FormItem>
        <Button type="dashed" long @click="handleAddExperience()" icon="plus-round">添加</Button>
      </FormItem>


      <FormItem>
        <h3 class="sub_title">项目经验</h3>
      </FormItem>

      <div v-for="(item, index) in formValidate.project" :key="index">
        <FormItem label="名称" prop="project.name">
          <Input v-model="item.name"/>
        </FormItem>

        <FormItem label="项目介绍" prop="project.project_desc">
          <Input v-model="item.project_desc"/>
        </FormItem>

        <FormItem label="担任职位" prop="project.work">
          <Input v-model="item.work"/>
        </FormItem>

        <Row>
          <i-col span="12">
            <FormItem label="开始时间" prop="project.begin_time">
              <DatePicker type="date" v-model="item.begin_time"></DatePicker>
            </FormItem>
          </i-col>
          <i-col span="12">
            <FormItem label="结束时间" prop="project.end_time">
              <DatePicker type="date" v-model="item.end_time"></DatePicker>
            </FormItem>
          </i-col>
        </Row>

        <FormItem>
          <Button type="dashed" long @click="handleRemove(formValidate.project, index)">删除</Button>
        </FormItem>
      </div>

      <FormItem>
        <Button type="dashed" long @click="handleAddProject()" icon="plus-round">添加</Button>
      </FormItem>


      <FormItem>
        <h3 class="sub_title">语言</h3>
      </FormItem>

      <div v-for="(item, index) in formValidate.language" :key="index">
        <FormItem label="名称" prop="language.name">
          <Input v-model="item.name"/>
        </FormItem>

        <FormItem label="证书" prop="language.certificate">
          <Input v-model="item.certificate"/>
        </FormItem>

        <FormItem>
          <Button type="dashed" long @click="handleRemove(formValidate.language, index)">删除</Button>
        </FormItem>
      </div>

      <FormItem>
        <Button type="dashed" long @click="handleAddLanguage()" icon="plus-round">添加</Button>
      </FormItem>


      <FormItem>
        <h3 class="sub_title">技能</h3>
      </FormItem>

      <div v-for="(item, index) in formValidate.skill" :key="index">
        <FormItem label="名称" prop="skill.name">
          <Input v-model="item.name"/>
        </FormItem>

        <FormItem label="时间" prop="skill.time">
          <Select v-model="item.time">
            <Option value="一年">一年</Option>
            <Option value="两年">两年</Option>
            <Option value="三年">三年</Option>
          </Select>
        </FormItem>

        <FormItem label="程度" prop="skill.level">
          <Select v-model="item.level">
            <Option value="了解">了解</Option>
            <Option value="一般">一般</Option>
            <Option value="熟练">熟练</Option>
          </Select>
        </FormItem>

        <FormItem>
          <Button type="dashed" long @click="handleRemove(formValidate.skill, index)">删除</Button>
        </FormItem>
      </div>

      <FormItem>
        <Button type="dashed" long @click="handleAddSkill()" icon="plus-round">添加</Button>
      </FormItem>


      <FormItem>
        <h3 class="sub_title">荣誉奖励</h3>
      </FormItem>

      <div v-for="(item, index) in formValidate.honor" :key="index">
        <FormItem label="名称" prop="honor.name">
          <Input v-model="item.name"/>
        </FormItem>

        <FormItem label="时间" prop="honor.time">
          <DatePicker type="date" v-model="item.time"></DatePicker>
        </FormItem>

        <FormItem>
          <Button type="dashed" long @click="handleRemove(formValidate.honor, index)">删除</Button>
        </FormItem>
      </div>

      <FormItem>
        <Button type="dashed" long @click="handleAddHonor()" icon="plus-round">添加</Button>
      </FormItem>


      <FormItem>
        <h3 class="sub_title">活动经历</h3>
      </FormItem>

      <div v-for="(item, index) in formValidate.activity" :key="index">
        <FormItem label="名称" prop="activity.name">
          <Input v-model="item.name"/>
        </FormItem>

        <FormItem label="时间" prop="activity.time">
          <DatePicker type="date" v-model="item.time"></DatePicker>
       </FormItem>

        <FormItem>
          <Button type="dashed" long @click="handleRemove(formValidate.activity, index)">删除</Button>
        </FormItem>
      </div>

      <FormItem>
        <Button type="dashed" long @click="handleAddActivity()" icon="plus-round">添加</Button>
      </FormItem>


      <FormItem style="margin-top:60px;">
        <Button type="primary" long @click="handleSubmit('formValidate')">提交修改</Button>
      </FormItem>
    </Form>

    <BackTop></BackTop>

  </div>

</template>
<script>
  import userRecruitApi from '../api/axios/userRecruitApi';

  export default {
    data () {
      return {
        formValidate: {
//          jobOrigin: "应届生求职网",
//          realName: "张立",
//          sex: "男",
//          hometown:"宁波",
//          birthday:"1996-01-16",
//          degree:"本科",
//          college:"杭州电子科技大学",
//          politics:"团员",
//          phone:"15958041842",
//          email:"530438645@163.com",
//          selfDescription: "1. 喜欢折腾，热爱编程 2. 为人踏实，乐于分享",
//          education: [{
//            school:"杭州电子科技大学",
//            major:"软件工程",
//            begin_time:"2014-09-11",
//            end_time:"2018-07-01"
//          },{
//            school:"杭州电子科技大学",
//            major:"软件工程",
//            begin_time:"2014-09-11",
//            end_time:"2018-07-01"
//          }],
//          workExperience: [{
//            company:"杭州数梦工场科技有限公司",
//            job:"Java工程师",
//            job_desc:"参与支付宝城市服务项目开发，整体项目是采用SOFA_Lite的分布式框架搭建的SOA架构的企业级应用；参与系统分析文档的编写；参与诚服线上问题的排查",
//            begin_time:"2017-06-26",
//            end_time:"2017-12-26"
//          }],
//          project: [{
//            name:"基于SOFA分布式架构的城市服务",
//            project_desc:"城市服务是支付宝推出的城市基础服务平台,让人人 可以享受到线上办理服务的便利。",
//            work:"我负责的是后端模块，项目是采用SOFA_Lite分布式框架搭建的，整体使用 Spring和Mybatis，各服务之间通过SOFA_RPC的方式来进行调用，其中也涉及到了多环境部署，定时服务，消息队列的场景。",
//            begin_time:"2017-07-26",
//            end_time:"2017-09-20"
//          },{
//            name:"基于AI算法的五子棋单双人游戏",
//            project_desc:"一个支持五子棋人机和玩家对战的游戏,玩家可以邀 请对手,悔棋和认输",
//            work:"AI部分采用了一步或多步预判的算法,其中包含了博弈树的深度遍历,极小值极大值算法,Alpha-beta剪枝,棋面估分,判赢等算法;双人部分用WebSocket维护在线列表,利用字典数据结构保证玩家不重名,利用进栈出栈思想进行下棋和悔棋",
//            begin_time:"2017-06-20",
//            end_time:"2017-07-10"
//          }],
//          language: [{
//            name:"英语",
//            certificate:"CET-6"
//          }],
//          skill: [{
//            name:"Java",
//            time:"三年",
//            level:"熟练"
//          },{
//            name:"MySQL",
//            time:"两年",
//            level:"熟练"
//          },{
//            name:"Linux",
//            time:"一年",
//            level:"一般"
//          }],
//          honor: [{
//            name:"奖学金",
//            time:"2016-09-15"
//          },{
//            name:"全国大学生“互联网+”创新大赛三等奖",
//            time:"2017-03-22"
//          },{
//            name:"浙江省第五届服务外包大赛三等奖",
//            time:"2016-06-15"
//          }],
//          activity: [{
//            name:"暑期社会实践",
//            time:"2015-06-15"
//          },{
//            name:"暑期社会实践",
//            time:"2015-06-15"
//          }],
//          attributes: []
        },
        ruleValidate: {

        }

      }
    },
    computed: {
      getUserId (){
        return this.$store.getters['auth/getUserId'];
      }
    },
    methods: {
      handleAddEducation () {
        this.formValidate.education.push({
          school: '',
          major: '',
          begin_time: '',
          end_time:''
        });
      },
      handleAddExperience(){
        this.formValidate.workExperience.push({
          company: '',
          job: '',
          job_desc: '',
          begin_time: '',
          end_time:''
        });
      },
      handleAddProject(){
        this.formValidate.project.push({
          name: '',
          project_desc: '',
          work: '',
          begin_time: '',
          end_time:''
        });
      },
      handleAddLanguage(){
        this.formValidate.language.push({
          name: '',
          certificate: ''
        });
      },
      handleAddSkill(){
        this.formValidate.skill.push({
          name: '',
          time: '',
          level: ''
        });
      },
      handleAddHonor(){
        this.formValidate.honor.push({
          name: '',
          time: ''
        });
      },
      handleAddActivity(){
        this.formValidate.activity.push({
          name: '',
          time: ''
        });
      },
      handleRemove (arr, index) {
        arr.splice(index, 1);
      },
      handleSubmit (formValidate) {
        this.$refs[formValidate].validate((valid) => {
          if (valid) {
            let that = this;

            this.formValidate.birthday = this.formatDate(this.formValidate.birthday);
            this.formValidate.education.map(function (item, index) {
              if (item.begin_time !== '' && item.end_time !== ''){
                item.begin_time = that.formatDate(item.begin_time);
                item.end_time = that.formatDate(item.end_time);
              }
              return item;
            });
            this.formValidate.workExperience.map(function (item, index) {
              if (item.begin_time !== '' && item.end_time !== ''){
                item.begin_time = that.formatDate(item.begin_time);
                item.end_time = that.formatDate(item.end_time);
              }
              return item;
            });
            this.formValidate.project.map(function (item, index) {
              if (item.begin_time !== '' && item.end_time !== ''){
                item.begin_time = that.formatDate(item.begin_time);
                item.end_time = that.formatDate(item.end_time);
              }
              return item;
            });
            this.formValidate.activity.map(function (item, index) {
              if (item.time !== ''){
                item.time = that.formatDate(item.time);
              }
              return item;
            });
            this.formValidate.honor.map(function (item, index) {
              if (item.time !== ''){
                item.time = that.formatDate(item.time);
              }
              return item;
            });

            let data = JSON.parse(JSON.stringify(this.formValidate));

            data.education = JSON.stringify(data.education);
            data.workExperience = JSON.stringify(data.workExperience);
            data.project = JSON.stringify(data.project);
            data.language = JSON.stringify(data.language);
            data.skill = JSON.stringify(data.skill);
            data.honor = JSON.stringify(data.honor);
            data.activity = JSON.stringify(data.activity);
            data.attributes = JSON.stringify(data.attributes);

            userRecruitApi.updateResume(
                data
            ,{
              success (res) {
                if (res.data.status === 200) {
                  that.$Message.success('更新简历成功');
                  that.$router.push('/user/resume_manage');
                }else {
                  that.$Message.success('更新简历失败');
                  console.log("updateResume：" + res.data);
                }
              }
            });

          } else {
            this.$Message.error('简历格式错误');
          }
        })
      },
      viewResume (){
        this.$router.push('/user/resume_manage');
      },
      formatDate(daytime){
        return daytime.getFullYear()+"-"+(daytime.getMonth()+1)+"-"+daytime.getDate();
      }
    },
    created() {
      let that = this;
      userRecruitApi.getResumeByUserId({
        userId: that.getUserId
      },{
        success (res) {
          if (res.data.status === 200) {

            res.data.data.education = JSON.parse(res.data.data.education);
            res.data.data.workExperience = JSON.parse(res.data.data.workExperience);
            res.data.data.project = JSON.parse(res.data.data.project);
            res.data.data.language = JSON.parse(res.data.data.language);
            res.data.data.skill = JSON.parse(res.data.data.skill);
            res.data.data.honor = JSON.parse(res.data.data.honor);
            res.data.data.activity = JSON.parse(res.data.data.activity);
            res.data.data.attributes = JSON.parse(res.data.data.attributes);

            delete res.data.data.isPush;
            delete res.data.data.isRead;
            delete res.data.data.remark;
            delete res.data.data.state;
            delete res.data.data.createTime;
            delete res.data.data.updateTime;

            that.formValidate = res.data.data;

          }else {
            console.log("getResumeByUserId：" + res.data);
          }
        }
      });
    }
  }
</script>
<style>
  .sub_title {
    font-size: 18px;
    width: 100%;
    text-align: center;
  }

  .del_btn {
    margin: auto;
  }
</style>
